﻿using System;
using System.Collections.Generic;

namespace Sustainalytics.Caching
{
    /// <summary>
    /// A cache which represents other cache.
    /// Links two caches.
    /// </summary>
    public class AppProxyCache : IAppCache
    {
        protected IAppCache ProxyCache { get; private set; }
        protected IAppCache Cache { get; private set; }

        public AppProxyCache(IAppCache proxyCache, IAppCache cache)
        {
            this.ProxyCache = proxyCache;
            this.Cache = cache;
        }

        public void Set(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            this.Cache.Set(key, value);
        }

        public void Set(string key, IEnumerable<string> value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            this.Cache.Set(key, value);
        }


        public void Add2Set(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }
            this.Cache.Add2Set(key, value);
        }


        public string GetString(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            string value = null;

            if (value == null)
            {
                value = this.Cache.GetString(key);
            }

            return value;
        }

        public IEnumerable<string> GetSet(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            IEnumerable<string> value = null; 

            if (value == null)
            {
                value = this.Cache.GetSet(key);
            }

            return value;
        }

        public void Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            this.ProxyCache.Remove(key);
            this.Cache.Remove(key);
        }

        public void Remove(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new CacheItemKeyNullOrEmptyException();
            }

            this.ProxyCache.Remove(key, value);
            this.Cache.Remove(key, value);
        }

        public void Clear()
        {
            this.ProxyCache.Clear();
        }

        public void Subscribe(Action<AppCacheItemAction> action)
        {
            this.ProxyCache.Subscribe(action);
        }


        public void CloseAll()
        {
        }
    }
}
